package cn.tedu.csmall.product.web;

import cn.tedu.csmall.product.ex.ServiceException;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.models.auth.In;
import lombok.Data;
import lombok.experimental.Accessors;

import static cn.tedu.csmall.product.web.ServiceCode.OK;

@Data
public class JsonResult {
    /**
     * 业务状态码
     */
    private Integer state;
    /**
     * 操作失败时的提示消息
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String message;


    public static JsonResult ok() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.state = ServiceCode.OK.getValue();
        return jsonResult;
    }

    public static JsonResult fail(ServiceException e) {
        // JsonResult jsonResult = new JsonResult();
        // jsonResult.state = e.getServiceCode().getValue();
        // jsonResult.message = e.getMessage();
        // return jsonResult;
        return fail(e.getServiceCode(), e.getMessage());
    }

    public static JsonResult fail(ServiceCode serviceCode, String message) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.state = serviceCode.getValue();
        jsonResult.message = message;
        return jsonResult;
    }


}
